use std::collections::HashMap;

use anyhow::Result;
use serde_json::{Value, to_value};
use http_client::h1::H1Client as Client;

use crate::node::{JsonRpcError, JsonRpcRequest, JsonRpcResponse, jsonrpc_request};
use crate::node::out::{
    SectorId,
    Job,
    SectorStorageInfo,
};


pub async fn sealing_jobs(client: &Client, url: &str, token: &str) -> Result<HashMap<String, Vec<Job>>> {
    let p = Vec::new();
    let req = JsonRpcRequest::with_params("Filecoin.WorkerJobs", p);

    let rsp = jsonrpc_request(client, url, token, req).await?;
    match rsp {
        None => Ok(HashMap::new()),
        Some(v) => Ok(v)
    }
}

pub async fn sector_storage(client: &Client, url: &str, token: &str, sector: SectorId) -> Result<Option<Vec<SectorStorageInfo>>> {
    let p = vec![serde_json::to_value(sector)?, Value::from(4i32), Value::from(0u64), Value::from(false)];
    let req = JsonRpcRequest::with_params("Filecoin.StorageFindSector", p);

    let rsp = jsonrpc_request(client, url, token, req).await?;
    Ok(rsp)
}